// import { title, messageDuration } from '@/config/settings';
import { setToken, getToken, removeToken } from '@/utils/auth';
import { setLocalData, getLocalData, removeLocalData } from '@/utils/localStore';

import { login, logout, getWalfareName, getAuthKeys } from '@/api/user';
// import { resetRouter } from '@/router';

const state = {
  userInfo: {},
  accessToken: '',
};
const getters = {
  userInfo(state) {
    if (Object.keys(state.userInfo).length <= 0 && getToken() !== '') {
      state.userInfo = getLocalData('userInfo') || {};
    }
    return Object.assign({}, state.userInfo);
  },
  accessToken(state) {
    if (state.accessToken === '' && getToken() !== '') {
      state.accessToken = getToken();
    }
    return state.accessToken;
  },
};

const mutations = {
  setAccessToken(state, accessToken) {
    state.accessToken = accessToken;
  },
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo;
  },
};

const actions = {
  // 由页面登录设置用户和授权信息
  async login({ dispatch }, payload) {
    const { data } = await login(payload);
    const { accessToken, accessTokenExpiresIn, userVo } = data;
    if (accessToken) {
      await dispatch('setAccessToken', {
        accessToken,
        accessTokenExpiresIn,
        userVo,
        isClient: false,
      });
      if (userVo.organId) {
        const { data = {} } = await getWalfareName(userVo.organId);
        setLocalData('welfareName', data.welfareName);
      }
      if (userVo.organId && userVo.id) {
        const { data = {} } = await getAuthKeys({
          authGroup: 417,
          enterpriseType: 100,
          organiId: userVo.organId,
          userId: userVo.id,
        });
        if (data.tree && Array.isArray(data.tree)) {
          setLocalData('menuList', data.tree);
        }
        if (data.buttonAuth && Array.isArray(data.buttonAuth)) {
          setLocalData(
            'authKeys',
            data.buttonAuth.reduce((col, key) => {
              col[key] = true;
              return col;
            }, {})
          );
        }
      }
    }
    return userVo;
  },
  // 由客户端设置用户和授权信息
  async setClientAuth({ dispatch }, payload) {
    const { token = {}, userInfo = {} } = payload;
    const { accessToken, accessTokenExpiresIn } = token;
    if (accessToken) {
      const [id, organId] = [userInfo.userId, userInfo.companyId];
      const userVo = { ...userInfo, id, organId, isClient: true };
      await dispatch('setAccessToken', {
        accessToken,
        accessTokenExpiresIn,
        userVo,
        isClient: true,
      });
      if (userVo.organId) {
        const { data = {} } = await getWalfareName(userVo.organId);
        setLocalData('welfareName', data.welfareName);
      }
      if (userVo.organId && userVo.id) {
        const { data = {} } = await getAuthKeys({
          authGroup: 417,
          enterpriseType: 100,
          organiId: userVo.organId,
          userId: userVo.id,
        });
        if (data.tree && Array.isArray(data.tree)) {
          setLocalData('menuList', data.tree);
        }
        if (data.buttonAuth && Array.isArray(data.buttonAuth)) {
          setLocalData(
            'authKeys',
            data.buttonAuth.reduce((col, key) => {
              col[key] = true;
              return col;
            }, {})
          );
        }
      }
    }
  },

  // 设置全部与授权和用户相关的数据
  async setAccessToken({ commit }, payload) {
    const { accessToken, accessTokenExpiresIn, userVo, isClient } = payload;
    commit('setAccessToken', accessToken);
    commit('setUserInfo', userVo);
    setToken(accessToken);
    setLocalData('userInfo', userVo, accessTokenExpiresIn);
    setLocalData('client', isClient, accessTokenExpiresIn);
  },

  async logout({ dispatch }) {
    await logout(state.accessToken);
    await dispatch('removeAccessToken');
    // await resetRouter();
  },

  async removeAccessToken({ commit }) {
    commit('setAccessToken', '');
    commit('setUserInfo', {});
    removeLocalData('userInfo');
    removeLocalData('client');
    removeToken();
  },
};

export default { state, getters, mutations, actions };
